package com.twu.biblioteca;




import java.io.Console;

import java.util.Scanner;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class Books {

    public static int DisplayAvailableBooks() {

        System.out.println("You choosed \n 1");
        try {
            System.out.println(" The following are the available books \n");
            System.out.println("================================================= \n");
            File fXmlFile = new File("C:/Users/chethan/twu-biblioteca-sandeshds/books.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            NodeList nList = doc.getElementsByTagName("books");
            doc.getDocumentElement().normalize();
            System.out.println("Name \t \t \t \t Author \t \t  Reg num \t  copies");
            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;

                    System.out.println(eElement.getElementsByTagName("name").item(0).getTextContent()+"\t"+eElement.getElementsByTagName("author").item(0).getTextContent()+"\t \t"+eElement.getElementsByTagName("rnum").item(0).getTextContent()+"\t \t \t"+eElement.getElementsByTagName("copies").item(0).getTextContent());

                }
            }
            System.out.println("================================================= \n");
        } catch (Exception e) {
            return 0;
        }

        return 1;

    }

    public static String MakeAReservation(String RegistrationNum)

    {
        if(Customer.flag == 0)
        {
            System.out.println("\n You choosed : 2 \n");
           System.out.println("\n Please login to continue \n");
            return null;
        }
        else
        {
        String Valid = "Thank You! Enjoy the book.";
        String InValid = "Sorry we don't have that book yet.";
        System.out.println("\n You choosed : 2 \n");
        int flag = 0;
        System.out.println("Enter the registration number of the book you want to reserve");
        System.out.println(" \n You entered Reg Num: "+RegistrationNum);
        try {
        File fXmlFile = new File("C:/Users/chethan/twu-biblioteca-sandeshds/books.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        NodeList nList = doc.getElementsByTagName("books");
        doc.getDocumentElement().normalize();
            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;

                    String Rnum = eElement.getElementsByTagName("rnum").item(0).getTextContent();
                    if(Rnum.equalsIgnoreCase(RegistrationNum))
                    {
                          System.out.println(Valid);
                        return Valid;
                    }

                }
            }

        } catch (Exception e) {
            return null;
        }
        System.out.println(InValid);
        return InValid;
        }

    }



}
